/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.sharding.sample.multidb.multitable.controller;

import com.lvyh.lightframe.sharding.sample.multidb.multitable.bean.TradeUserBean;
import com.lvyh.lightframe.sharding.sample.multidb.multitable.bean.TradeUserParamInfo;
import com.lvyh.lightframe.sharding.sample.multidb.multitable.service.TradeUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/trade-user")
@Slf4j
public class TradeUserController {

    @Autowired
    private TradeUserService tradeUserService;

    /**
     * http://localhost:8080/trade-user/insert
     */
    @RequestMapping("/insert")
    public void insert() throws Exception {
        TradeUserBean tradeUserBean = new TradeUserBean();
        tradeUserBean.setId(1000L);
        tradeUserBean.setNickname("梅西");
        tradeUserBean.setAge(31);
        tradeUserBean.setBirthday("2021-10-21");
        tradeUserBean.setSex(1);
        tradeUserBean.setPassword("111111");
        tradeUserService.insert(tradeUserBean);

        tradeUserBean = new TradeUserBean();
        tradeUserBean.setId(1001L);
        tradeUserBean.setNickname("C罗");
        tradeUserBean.setAge(32);
        tradeUserBean.setBirthday("2021-10-20");
        tradeUserBean.setSex(1);
        tradeUserBean.setPassword("222222");
        tradeUserService.insert(tradeUserBean);
    }

    /**
     * http://localhost:8080/trade-user/update
     */
    @RequestMapping("/update")
    public void update() throws Exception {
        TradeUserBean tradeUserBean = new TradeUserBean();
        tradeUserBean.setId(10013L);
        tradeUserBean.setNickname("梅西");
        tradeUserBean.setAge(32);
        tradeUserBean.setBirthday("2021-10-22");
        tradeUserBean.setSex(0);
        tradeUserBean.setPassword("111000");
        boolean result = tradeUserService.update(tradeUserBean);
        log.info("update user:{}", result);
    }

    //http://localhost:8080/trade-user/delete
    @GetMapping("delete")
    public void delete() throws Exception {
        TradeUserBean tradeUserBean = new TradeUserBean();
        tradeUserBean.setId(10014L);
        boolean result = tradeUserService.deleteUser(tradeUserBean);
        log.info("delete user:{}", result);
    }

    //http://localhost:8080/trade-user/selectById
    @GetMapping("selectById")
    public void selectById() throws Exception {
        TradeUserBean tradeUserBean = new TradeUserBean();
        tradeUserBean.setId(10018L);
        TradeUserBean result = tradeUserService.selectById(tradeUserBean);
        log.info("selectById:{}", result != null ? result.getAge() : "");
    }

    //http://localhost:8080/trade-user/selectList
    @GetMapping("selectList")
    public void selectList() throws Exception {
        TradeUserParamInfo param = new TradeUserParamInfo();
        param.setId(10015L);//带有分片键
        List<TradeUserBean> userList = tradeUserService.selectList(param);
        log.info("selectList:{}", userList.size());
    }

    //http://localhost:8080/trade-user/selectListByPage
    @GetMapping("selectListByPage")
    public void selectListByPage() throws Exception {
        TradeUserParamInfo param = new TradeUserParamInfo();
        param.setId(10015L);//带有分片键
        param.setPageNum(1);
        param.setPageSize(5);
        List<TradeUserBean> userList = tradeUserService.selectListByPage(param);
        log.info("selectListByPage:{}", userList);
    }

    //http://localhost:8080/trade-user/selectList2
    @GetMapping("selectList2")
    public void selectList2() throws Exception {
        TradeUserParamInfo param = new TradeUserParamInfo();
        //param.setId(10015L);//不带分片键
        List<TradeUserBean> userList = tradeUserService.selectList(param);
        log.info("selectList:{}", userList.size());
    }

    //http://localhost:8080/trade-user/selectListByPage2
    @GetMapping("selectListByPage2")
    public void selectListByPage2() throws Exception {
        TradeUserParamInfo param = new TradeUserParamInfo();
        //param.setId(10015L);//不带分片键
        param.setPageNum(1);
        param.setPageSize(5);
        param.setOrderColumns("update_time desc");
        List<TradeUserBean> userList = tradeUserService.selectListByPage(param);
        log.info("selectListByPage:{}", userList.size());
    }

    //http://localhost:8080/trade-user/selectListByPage3
    @GetMapping("selectListByPage3")
    public void selectListByPage3() throws Exception {

        TradeUserParamInfo param = new TradeUserParamInfo();
        //param.setId(10015L);//不带分片键
        param.setPageNum(2);  //page num>1 ,will rewrite sql: limit 0 , page_num * page_size
        param.setPageSize(5);
        param.setOrderColumns("update_time desc");
        List<TradeUserBean> userList = tradeUserService.selectListByPage(param);
        log.info("selectListByPage:{}", userList.size());
    }

}
